package com.pocketsweet.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.MLContext;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.chatlib.controller.HXSDKHelper;
import com.pocketsweet.ui.uilib.MyAlertDialog;
import com.pocketsweet.ui.uilib.wheelview.ScreenInfo;
import com.pocketsweet.ui.uilib.wheelview.WheelBoredTime;

@ContentView(R.layout.activity_message_setting)
public class MessageSetting extends BaseActivity {

	@ViewInject(R.id.checkBtnAcceptMessage)
	private ToggleButton checkBtnAcceptMessage;

	@ViewInject(R.id.checkBtnVoice)
	private ToggleButton checkBtnVoice;

	@ViewInject(R.id.checkBtnShake)
	private ToggleButton checkBtnShake;

	@ViewInject(R.id.checkBtnBorder)
	private ToggleButton checkBtnBorder;

	@ViewInject(R.id.relBored)
	private RelativeLayout relBored;

	@ViewInject(R.id.tvBordTimeStart)
	private TextView tvBordTimeStart;

	@ViewInject(R.id.tvBordTimeEnd)
	private TextView tvBordTimeEnd;

	private EMChatOptions chatOptions;
	WheelBoredTime wheelBoredTime;

	public static final String MESSAGE_ACCEPT = "message_accept";
	public static final String MESSAGE_VOICE = "message_voice";
	public static final String MESSAGE_SHAKE = "message_shake";
	public static final String MESSAGE_BORDER = "message_border";
	public static final String MESSAGE_BORDER_START = "message_border_start";
	public static final String MESSAGE_BORDER_END = "message_border_end";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initCheckListener();
		tvBordTimeStart.setText(MLContext
				.getBorederTimeMessage("message_border_start") + ":00 ~");
		tvBordTimeEnd
				.setText(" "
						+ MLContext.getBorederTimeMessage("message_border_end")
						+ ":00");
	}

	private void initCheckListener() {
		chatOptions = EMChatManager.getInstance().getChatOptions();
		// if(chatOptions.getNotificationEnable()){
		// Log.e("==1111=", "11111111111");
		// }else{
		// Log.e("==1111=", "00000000000");//不接受通知时打印
		// }
		if (MLContext.getMessage(MESSAGE_ACCEPT)) {
			checkBtnAcceptMessage.setChecked(true); // 设置开关初始状态
		} else {
			checkBtnAcceptMessage.setChecked(false);

		}
		if (MLContext.getMessage(MESSAGE_VOICE)) {
			checkBtnVoice.setChecked(true); // 设置开关初始状态
		} else {
			checkBtnVoice.setChecked(false);

		}
		if (MLContext.getMessage(MESSAGE_SHAKE)) {
			checkBtnShake.setChecked(true); // 设置开关初始状态
		} else {
			checkBtnShake.setChecked(false);

		}
		if (MLContext.getMessage(MESSAGE_BORDER)) {
			checkBtnBorder.setChecked(true); // 设置开关初始状态
		} else {
			checkBtnBorder.setChecked(false);

		}
		checkBtnAcceptMessage
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean checkState) {
						if (checkState) {
							checkBtnVoice.setEnabled(true);
							checkBtnShake.setEnabled(true);
							checkBtnBorder.setEnabled(true);
							checkBtnVoice.setChecked(true);
							checkBtnShake.setChecked(true);
							checkBtnBorder.setChecked(false);
							MLContext.saveMessage(MESSAGE_ACCEPT, true);
							MLContext.saveMessage(MESSAGE_SHAKE, true);
							MLContext.saveMessage(MESSAGE_VOICE, true);
							MLContext.saveMessage(MESSAGE_BORDER, false);
							chatOptions.setNotificationEnable(true);
							EMChatManager.getInstance().setChatOptions(
									chatOptions);
							chatOptions.setShowNotificationInBackgroud(true);
							HXSDKHelper.getInstance().getModel()
									.setSettingMsgNotification(true);
						} else {
							checkBtnVoice.setEnabled(false);
							checkBtnShake.setEnabled(false);
							checkBtnBorder.setEnabled(false);
							checkBtnVoice.setChecked(false);
							checkBtnShake.setChecked(false);
							checkBtnBorder.setChecked(false);
							MLContext.saveMessage(MESSAGE_ACCEPT, false);
							MLContext.saveMessage(MESSAGE_SHAKE, false);
							MLContext.saveMessage(MESSAGE_VOICE, false);
							MLContext.saveMessage(MESSAGE_BORDER, false);
							MLContext.saveMessage(MESSAGE_BORDER, false);
							chatOptions.setNotificationEnable(false);
							chatOptions.setShowNotificationInBackgroud(false);
							EMChatManager.getInstance().setChatOptions(
									chatOptions);
							HXSDKHelper.getInstance().getModel()
									.setSettingMsgNotification(false);
						}

					}
				});
		checkBtnVoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean checkState) {
				if (checkState) {
					MLContext.saveMessage(MESSAGE_VOICE, true);
					chatOptions.setNoticeBySound(true);
					EMChatManager.getInstance().setChatOptions(chatOptions);
					HXSDKHelper.getInstance().getModel()
							.setSettingMsgSound(true);

				} else {
					MLContext.saveMessage(MESSAGE_VOICE, false);
					chatOptions.setNoticeBySound(false);
					EMChatManager.getInstance().setChatOptions(chatOptions);
					HXSDKHelper.getInstance().getModel()
							.setSettingMsgSound(false);
				}

			}
		});
		checkBtnShake.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean checkState) {
				if (checkState) {
					MLContext.saveMessage(MESSAGE_SHAKE, true);
					chatOptions.setNoticedByVibrate(true);
					EMChatManager.getInstance().setChatOptions(chatOptions);
					HXSDKHelper.getInstance().getModel()
							.setSettingMsgVibrate(true);

				} else {
					MLContext.saveMessage(MESSAGE_SHAKE, false);
					chatOptions.setNoticedByVibrate(false);
					EMChatManager.getInstance().setChatOptions(chatOptions);
					HXSDKHelper.getInstance().getModel()
							.setSettingMsgVibrate(false);

				}

			}
		});
		checkBtnBorder
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean checkState) {
						if (checkState) {
							MLContext.saveMessage(MESSAGE_BORDER, true);
						} else {
							MLContext.saveMessage(MESSAGE_BORDER, false);
						}
						Intent intent = new Intent();
						intent.setAction(Main.MESSAGE_BORDER);
						sendBroadcast(intent);
					}
				});
		relBored.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				LayoutInflater inflater = LayoutInflater
						.from(MessageSetting.this);
				final View timepickerview = inflater.inflate(
						R.layout.timepicker, null);
				ScreenInfo screenInfo = new ScreenInfo(MessageSetting.this);
				wheelBoredTime = new WheelBoredTime(timepickerview);
				wheelBoredTime.screenheight = screenInfo.getHeight();
				wheelBoredTime.initDateTimePicker();
				final MyAlertDialog dialog = new MyAlertDialog(
						MessageSetting.this).builder().setTitle("勿扰时段设置")
						.setView(timepickerview)
						.setNegativeButton("取消", new OnClickListener() {
							@Override
							public void onClick(View v) {

							}
						});
				dialog.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {
						int startTime;
						int endTime;
						startTime = wheelBoredTime.getStartTime();
						endTime = wheelBoredTime.getEndTime();
						MLContext.saveMessage(MESSAGE_BORDER_START, startTime);
						MLContext.saveMessage(MESSAGE_BORDER_END, endTime);
						tvBordTimeStart.setText(startTime + ":00 ~");
						tvBordTimeEnd.setText(" " + endTime + ":00");
					}
				});
				dialog.show();

			}
		});
	}

	public void back(View v) {
		MessageSetting.this.finish();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

}
